package com.example.jpademo.service;

import com.example.jpademo.UserVo;
import com.example.jpademo.entity.User;
import com.example.jpademo.entity.UserDto;
import com.example.jpademo.repository.UserDao;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Predicate;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @className: UserServiceImpl
 * @description:
 * @author: sh.Liu
 * @date: 2022-01-17 13:56
 */
@Service
public class UserServiceImpl {

    @Transactional
    public void test() {
        User user = userDao.findById(1).get();
        String newName = "tom";

        System.out.println("user name: " + user.getName());
        System.out.println("user age: " + user.getAge());
        System.out.println("name will be updated to " + newName);

        userDao.updateUserName(newName, 1);

        User user1 = userDao.findById(1).get();

        System.out.println("user1 age: " + user1.getAge());
        System.out.println("user1 name: " + user1.getName());
        System.out.println("age will be updated to " + 18);
        user1.setAge(18);
        userDao.save(user1);
//        System.out.println("-------------");
//        System.out.println(userDao.findById(1).get());
    }
    @Transactional
    public void test1() {
        User user1 = userDao.findById(1).get();

        System.out.println("user1 age: " + user1.getAge());
        System.out.println("user1 name: " + user1.getName());
    }

    private final UserDao userDao;

    public UserServiceImpl(UserDao userDao) {
        this.userDao = userDao;
    }

    public Page<User> page(PageRequest pageRequest) {
        return userDao.findAll(pageRequest);
    }

    public List<User> query() {
        return userDao.findAll((root, query, criteriaBuilder) -> {
            ArrayList<Predicate> predicates = new ArrayList<>();
            predicates.add(criteriaBuilder.like(root.get("name"), "aa%"));
            predicates.add(criteriaBuilder.greaterThan(root.get("maxSalary"), 10));
//            return query.where(predicates.toArray(new Predicate[predicates.size()])).getRestriction();

            return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
        });
//        return null;
    }

    public User getUserById(Integer id) {
        return userDao.getById(id);
    }

    public User getUserBySalary(Integer salary) {
        return userDao.getUserBySalary(salary);
    }

    public List<User> listUser() {
        User user = new User();
        user.setMaxSalary(15);

        ExampleMatcher matcher = ExampleMatcher.matching()
                .withMatcher("maxSalary", matcher1 -> matcher1.startsWith())//模糊查询匹配开头，即{username}%
                ;//忽略字段，即不管password是什么值都不加入查询条件
        Example<User> example = Example.of(user, matcher);
        return userDao.findAll(example);
    }

    public List<UserVo> listUserVo(Integer salary) {
        return userDao.listUserVo(salary);
    }
    public List<User> maxSalary(Integer salary) {
        return userDao.maxSalary(salary);
    }

    public List<String> names(Integer salary) {
        return userDao.names(salary);
    }

    public int save(User user) {
        userDao.save(user);
        return user.getId();
    }

    public int update(Integer id, User user) {
        // 有id则更新，无id则新增
        user.setId(id);
        userDao.save(user);
        return id;
    }

    public int delete(Integer id) {
        User user = new User();
        user.setId(id);
        userDao.delete(user);
        return id;
    }
}